local extension = Package:new("jin_heg")
extension.extensionName = "new_heg"
extension.game_modes_whitelist = { 'mul_heg_mode' }

local H = require "packages/new_heg/util"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["jin_heg"] = "紫气东来",
--   ["of_heg"] = "线下",
}

local simayif = General(extension, "hs__n_simayif", "jinz", 4)
simayif:addCompanions("hs__n_zhangchunhuaf")
local yingshi = fk.CreateTriggerSkill{
  name = "hs__n_yingshi",
  anim_type = "special",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Play
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#hs__n_yingshi_choose", self.name, true)
    if #to > 0 then
      self.cost_data = {to = to[1]}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local user = room:getPlayerById(self.cost_data.to)
    local tos = room:askForChoosePlayers(player, table.map(table.filter(room.alive_players, function (p) return p ~= user end), Util.IdMapper), 1, 1, "#hs__n_yingshi_choose_use", self.name, false)
    local to = room:getPlayerById(tos[1])
    room:useVirtualCard("known_both", nil, user, to, self.name, true)
    if user ~= player and not player.dead then
      player:drawCards(1, self.name)
    end
  end,
}
local shunfu = fk.CreateActiveSkill{
  name = "hs__n_shunfu",
  anim_type = "control",
  frequency = Skill.Limited,
  prompt = "#hs__n_shunfu_prompt",
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryGame) < 1 and table.find(Fk:currentRoom().alive_players, function (p) return  H.getKingdom(p) == "unknown" and p.id ~= player.id end)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return #selected < 3 and H.getKingdom(Fk:currentRoom():getPlayerById(to_select)) == "unknown" and to_select ~= player.id
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    for _, to in ipairs(effect.tos) do
      local target = room:getPlayerById(to)
      target:drawCards(2, self.name)
    end
    if player.dead then return end
    for _, to in ipairs(effect.tos) do
      local target = room:getPlayerById(to)
      local use = U.askForPlayCard(room, target, nil, "slash", self.name, "#hs__n_shunfu_askForSlash", {bypass_distances = true}, true)
      if use then
        use.disresponsiveList = use.disresponsiveList or {}
        table.insertTableIfNeed(use.disresponsiveList, table.map(room:getAlivePlayers(), Util.IdMapper))
        room:useCard(use)
      end
    end
  end
}
simayif:addSkill(yingshi)
simayif:addSkill(shunfu)
Fk:loadTranslationTable{
  ["hs__n_simayif"] = "司马懿",
  ["#hs__n_simayif"] = "应期佐命",
  ["designer:hs__n_simayif"] = "",
  ["illustrator:hs__n_simayif"] = "",
  
  ["hs__n_yingshi"] = "鹰视",
  [":hs__n_yingshi"] = "出牌阶段开始时，你可令一名角色视为对你选择的另一名角色使用【知己知彼】，然后若此牌的使用者不为你，你摸一张牌。",
  ["hs__n_shunfu"] = "瞬覆",
  [":hs__n_shunfu"] = "限定技，出牌阶段，你可令至多三名未确定势力的其他角色各摸两张牌，然后这些角色依次选择是否使用一张无距离限制且不可响应的【杀】。",
  ["#hs__n_yingshi_choose"] = "鹰视：选择一名角色，其视为对你选择的另一名角色使用【知己知彼】",
  ["#hs__n_yingshi_choose_use"] = "鹰视：选择【知己知彼】的目标",

  ["~hs__n_simayif"] = "",
}

local zhangchunhuaf = General(extension, "hs__n_zhangchunhuaf", "jinz", 3, 3, General.Female)
local ejue = fk.CreateTriggerSkill{
  name = "hs__n_ejue",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and H.getGeneralsRevealedNum(data.to) == 0 and data.card.trueName == "slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.damage = data.damage + 1
  end,
}
local shangshi = fk.CreateTriggerSkill{
  name = "hs__n_shangshi",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function (self, event, target, player, data)
    local diff = (player.maxHp - player.hp) - player:getHandcardNum()
    if diff > 0 then
      player:drawCards(diff, self.name)
    end
  end,
}
zhangchunhuaf:addSkill(ejue)
zhangchunhuaf:addSkill(shangshi)
Fk:loadTranslationTable{
  ["hs__n_zhangchunhuaf"] = "张春华",
  ["#hs__n_zhangchunhuaf"] = "锋刃染霜",
  ["designer:hs__n_zhangchunhuaf"] = "",
  ["illustrator:hs__n_zhangchunhuaf"] = "",
  
  ["hs__n_ejue"] = "扼绝",
  [":hs__n_ejue"] = "锁定技，当你使用【杀】对未确定势力角色造成伤害时，此伤害+1。",
  ["hs__n_shangshi"] = "伤逝",
  [":hs__n_shangshi"] = "每回合结束时，你可将手牌摸至你已损失体力值",

  ["~hs__n_zhangchunhuaf"] = "",
}

local simashi = General(extension, "hs__n_simashi", "jinz", 4)
simashi:addCompanions("hs__n_yanghuiyu")
local yimie = fk.CreateViewAsSkill{
  name = "hs__n_yimie",
  pattern = "peach",
  prompt = "#hs__n_yimie_prompt_use_peach",
  attached_skill_name = "#hs__n_yimie_viewAs&",
  card_filter = function (self, to_select, selected, player)
    return #selected < 1 and Fk:getCardById(to_select).color == Card.Red and table.contains(player:getCardIds("h"),to_select)
  end,
  view_as = function (self, cards, player)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("peach")
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function (self, player, response)
    return not response and table.find(Fk:currentRoom().alive_players, function(p)
      return p.phase ~= Player.NotActive and p:hasShownSkill(self)
    end) and table.find(Fk:currentRoom().alive_players, function(p)
      return p.dying and H.compareKingdomWith(p, player, true)
    end) 
  end
}
local yimie_trigger = fk.CreateTriggerSkill{
  name = "#hs__n_yimie_trigger",
  anim_type = "offensive",
  frequency = Skill.Compulsory,

  refresh_events = {fk.EnterDying},
  can_refresh = function(self, event, target, player, data)
    return player:hasShownSkill(self) and player.phase ~= Player.NotActive
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, self.name)
    player:broadcastSkillInvoke(self.name)
  end,
}
local yimie_prohibit = fk.CreateProhibitSkill{
  name = "#hs__n_yimie_prohibit",
  prohibit_use = function(self, player, card)
    if card.name == "peach" then
      return table.find(Fk:currentRoom().alive_players, function(p)
        return p.phase ~= Player.NotActive and p:hasShownSkill(yimie)
      end) and table.find(Fk:currentRoom().alive_players, function(p)
        return p.dying and H.compareKingdomWith(p, player)
      end)
    end
  end,
}
local yimie_viewAs = fk.CreateViewAsSkill{
  name = "#hs__n_yimie_viewAs&",
  pattern = "peach",
  prompt = "#hs__n_yimie_prompt_use_peach",
  mute = true,
  card_filter = function (self, to_select, selected, player)
    return #selected < 1 and Fk:getCardById(to_select).color == Card.Red and table.contains(player:getCardIds("h"),to_select)
  end,
  view_as = function (self, cards, player)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("peach")
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  after_use = function (self, player, use)
    local p = table.find(player.room:getAlivePlayers(), function(p)
      return p.phase ~= Player.NotActive and p:hasShownSkill(yimie)
    end)
    if not p then return end
    player.room:notifySkillInvoked(p, yimie.name)
    p:broadcastSkillInvoke(yimie.name)
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function (self, player, response)
    return not response and table.find(Fk:currentRoom().alive_players, function(p)
      return p.phase ~= Player.NotActive and p:hasShownSkill(yimie)
    end) and table.find(Fk:currentRoom().alive_players, function(p)
      return p.dying and H.compareKingdomWith(p, player, true)
    end) 
  end
}
local ruilve = fk.CreateTriggerSkill{
  name = "hs__n_ruilve",
  mute = true,
  attached_skill_name = "hs__n_ruilve_other&",
  refresh_events = {fk.AfterPropertyChange, fk.GeneralRevealed, fk.GeneralHidden, fk.Deathed},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local players = room.alive_players
    local targets_map = {}
    for _, p in ipairs(players) do
      local will_attach = false
      if p ~= player and p.kingdom == "unknown" then
        will_attach = true
      end
      targets_map[p] = will_attach
    end
    for p, v in pairs(targets_map) do
      if v ~= p:hasSkill("hs__n_ruilve_other&") then
        room:handleAddLoseSkills(p, v and self.attached_skill_name or "-" .. self.attached_skill_name, nil, false, true)
      end
    end
  end,
  on_acquire = function(self, player)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p ~= player and p.kingdom == "unknown" then
        room:handleAddLoseSkills(p, self.attached_skill_name, nil, false, true)
      end
    end
  end,
  on_lose = function (self, player, is_death)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p ~= player and p.kingdom ~= "unknown" then
        room:handleAddLoseSkills(p, "-" .. self.attached_skill_name, nil, false, true)
      end
    end
  end,
}
local ruilveother = fk.CreateActiveSkill{ --暗将获得的
  name = "hs__n_ruilve_other&",
  anim_type = "support",
  prompt = "#hs__n_ruilve_ask",
  card_num = 1,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player:hasSkill(self)
  end,
  card_filter = function(self, to_select, selected, player)
    return #selected == 0 and Fk:getCardById(to_select).is_damage_card
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    if #effect.cards > 0 then
    player:showCards(effect.cards)
    end
    local targets = table.filter(room:getOtherPlayers(player, false), function(p) return p:hasShownSkill(hs__n_ruilve) end)
    if #targets == 0 then return false end
    local to
    if #targets == 1 then
      to = targets[1]
    else
      to = room:getPlayerById(room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, nil, self.name, false)[1])
    end
    room:doIndicate(player.id, {to.id})
    room:moveCardTo(effect.cards, Player.Hand, to, fk.ReasonGive, self.name, nil, false, to.id)
    player:drawCards(1,self.name)
  end,
}
Fk:addSkill(yimie_viewAs)
Fk:addSkill(ruilveother)
yimie:addRelatedSkill(yimie_trigger)
yimie:addRelatedSkill(yimie_prohibit)
simashi:addSkill(yimie)
simashi:addSkill(ruilve)
Fk:loadTranslationTable{
  ["hs__n_simashi"] = "司马师",
  ["#hs__n_simashi"] = "",
  ["designer:hs__n_simashi"] = "",
  ["illustrator:hs__n_simashi"] = "",
  
  ["hs__n_yimie"] = "夷灭",
  [":hs__n_yimie"] = "锁定技，你的回合内，与处于濒死状态角色势力相同/势力不同的角色不能使用【桃】/可将一张红桃手牌当【桃】对处于濒死状态的角色使用。",
  ["hs__n_ruilve"] = "睿略",
  [":hs__n_ruilve"] = "未确定势力的其他角色出牌阶段限一次，其可展示并交给你一张伤害类牌，然后其摸一张牌。",
  ["#hs__n_yimie_prompt_use_peach"] = "夷灭：你可将一张红桃手牌对濒死状态角色使用",
  ["#hs__n_yimie_trigger"] = "夷灭",
  ["#hs__n_yimie_prohibit"] = "夷灭",
  ["#hs__n_yimie_viewAs&"] = "夷灭",
  ["#hs__n_ruilve-ask"] = "睿略: 选择一张伤害类牌",
  ["hs__n_ruilve_other&"] =  "睿略",
  [":hs__n_ruilve_other&"] = "出牌阶段限一次，你可以展示并交给司马师一张伤害类牌，然后你摸一张牌。",

  ["~hs__n_simashi"] = "",
}

local simazhaof = General(extension, "hs__n_simazhaof", "jinz", 3)
simazhaof:addCompanions("hs__n_wangyuanji")
local zhaoran = fk.CreateTriggerSkill{
  name = "hs__n_zhaoran",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and player.phase == Player.Play and target == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local kingdoms = {}
    for _, p in ipairs(table.filter(Fk:currentRoom().alive_players, function(p) return  p.kingdom ~= "unknown" and p.kingdom ~= "wild" end)) do
     table.insertIfNeed(kingdoms, p.kingdom)
    end
    local n = 4 - #kingdoms -- 5个势力
    player:drawCards(n, self.name)
    for _, p in ipairs(room:getAlivePlayers()) do
      if p.kingdom == "unknown" and not p.dead then
        local all_choices = {"revealMain:::" .. p:getMark("__heg_general"), "revealDeputy:::" .. p:getMark("__heg_deputy"), "Cancel"}
        if p:prohibitReveal() then
          table.remove(all_choices, "revealMain:::" .. p:getMark("__heg_general"))
        end
        if p:prohibitReveal(true) then
          table.remove(all_choices, "revealDeputy:::" .. p:getMark("__heg_deputy"))
        end
        local choice = room:askForChoice(p,all_choices, self.name)
        if choice == "revealMain:::" .. p:getMark("__heg_general") then
          p:revealGeneral(false)
          room:endTurn()
          return true
        elseif choice =="revealDeputy:::" .. p:getMark("__heg_deputy") then
          p:revealGeneral(true)
          room:endTurn()
          return true
        end
      end
    end
  end,
}
local beiluan_filter = fk.CreateFilterSkill{
  name = "#hs__n_beiluan_filter",
  card_filter = function (self, card, player)
    return table.contains(player:getCardIds("h"), card.id) and card.type ~= Card.TypeEquip and player:getMark("hs__n_beiluan_viewAsSlash-turn") > 0
  end,
  view_as = function (self, card, player)
    local c = Fk:cloneCard("slash", card.suit, card.number)
    c.skillName = "hs__n_beiluan"
    return c
  end
}
local beiluan = fk.CreateTriggerSkill{
  name = "hs__n_beiluan",
  anim_type = "defensive",
  events = {fk.Damaged},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.from
  end,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(data.from, "hs__n_beiluan_viewAsSlash-turn", 1)
  end
}
beiluan:addRelatedSkill(beiluan_filter)
simazhaof:addSkill(zhaoran)
simazhaof:addSkill(beiluan)
Fk:loadTranslationTable{
  ["hs__n_simazhaof"] = "司马昭",
  ["#hs__n_simazhaof"] = "天下畏威",
  ["designer:hs__n_simazhaof"] = "",
  ["illustrator:hs__n_simazhaof"] = "",
  
  ["hs__n_zhaoran"] = "昭然",
  [":hs__n_zhaoran"] = "出牌阶段开始时，你可摸X张牌（X为除野心家外缺少的势力数），然后未确定势力的角色依次选择是否明置一张武将牌并令你结束此回合。",
  ["hs__n_beiluan"] = "备乱",
  [":hs__n_beiluan"] = "当你受到伤害后，你可令伤害来源不为装备牌的手牌均视为【杀】直至此回合结束。",
  ["#hs__n_beiluan_filter"] = "备乱",

  ["~hs__n_simazhaof"] = "",
}

local simazhou = General(extension, "hs__n_simazhou", "jinz", 4)
local pojing = fk.CreateActiveSkill{
  name = "hs__n_pojing",
  prompt = "#hs__n_pojing-active",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function() return false end,
  target_num = 1,
  target_filter = function(self, to_select, selected, _, _, _, player)
    return #selected == 0 and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local choices = {}
    if not target:isAllNude() then
      table.insert(choices, "hs__n_pojing_getcard")
    end
    table.insert(choices, "hs__n_pojing_summmon")
    local choice = room:askForChoice(target,choices,self.name, "#hs__n_pojing-choice")
    if choice == "hs__n_pojing_getcard" then
      local cid = room:askForCardChosen(player, target, "hej", self.name)
      room:obtainCard(player, cid, false, fk.ReasonPrey)
    else
      local kingdom = player.kingdom
      local targets = table.map(table.filter(room.alive_players, function(p) return (p.kingdom == "unknown" or p.kingdom == kingdom) and not p.dead and H.getGeneralsRevealedNum(p) < 2 end), Util.IdMapper)
      room:sortPlayersByAction(targets)
      for _, pid in ipairs(targets) do
        local p = room:getPlayerById(pid)
        if (p.kingdom == "unknown" or p.kingdom == kingdom) and not p.dead and H.getGeneralsRevealedNum(p) < 2 then
          local x = H.getGeneralsRevealedNum(p) 
          --君主启用 if H.getKingdomPlayersNum(room)[kingdom] >= #room.players // 2 and not table.find(room.alive_players, function(_p) return _p.general == "ld__lordsimayi" end) then break end
          local main, deputy = false, false
          if H.compareExpectedKingdomWith(p, player) then
            local general = Fk.generals[p:getMark("__heg_general")]
            main = general.kingdom == kingdom or general.subkingdom == kingdom
            general = Fk.generals[p:getMark("__heg_deputy")]
            deputy = general.kingdom == kingdom or general.subkingdom == kingdom
          end
          if H.askForRevealGenerals(room, p, self.name, main, deputy) == true then
            local y = H.getGeneralsRevealedNum(p)
            if (y - x) > 0 and not p.dead then
              room:damage{
                from = p,
                to = target,
                damage = y - x,
                skillName = self.name
              }
            end
          end
        end
      end
    end
  end,
}


simazhou:addSkill(pojing)
Fk:loadTranslationTable{
  ["hs__n_simazhou"] = "司马伷",
  ["#hs__n_simazhou"] = "温恭的狻猊",
  ["designer:hs__n_simazhou"] = "",
  ["illustrator:hs__n_simazhou"] = "",
  
  ["hs__n_pojing"] = "迫境",
  [":hs__n_pojing"] = "出牌阶段限一次，你可令一名其他角色选择一项：1.你获得其区域内一张牌；2.你令所有与你势力相同且有未明置武将牌的角色，或明置武将牌后将与你势力相同的角色依次选择是否明置任意张武将牌并对其造成等量伤害。",
  ["#hs__n_pojing-active"] = "迫境: 选择一名其他角色，其选择你获得其牌或你发起势力召唤",
  ["hs__n_pojing_getcard"] = "令其获得你区域的一张牌",
  ["hs__n_pojing_summmon"] = "令其发动势力召唤",
  ["#hs__n_pojing-choice"] = "迫境: 选择一项",

  ["~hs__n_simazhou"] = "",
}

local simaliang = General(extension, "hs__n_simaliang", "jinz", 4)
local gongzhi = fk.CreateTriggerSkill{
  name = "hs__n_gongzhi",
  anim_type = "drawcard",
  events = {fk.EventPhaseChanging},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and data.to == Player.Draw
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:skip(Player.Draw)
    local players = room:getAlivePlayers()
    local x = 0
    while x < 4 do
      for _, p in ipairs(players) do
        if H.compareKingdomWith(player, p) and not p.dead then
          p:drawCards(1, self.name)
          x = x + 1
          if x >= 4 then
            break
          end
        end
      end
      if not table.find(players, function (p) return H.compareKingdomWith(player, p) and not p.dead end) then
        break
      end
    end
  end,
}
local shenju = fk.CreateTriggerSkill{
  name = "hs__n_shenju",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.GeneralRevealed},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player ~= target and H.compareKingdomWith(player, target) and player.hp < player.maxHp
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name,
    })
    player:throwAllCards("h")
  end
}
simaliang:addSkill(gongzhi)
simaliang:addSkill(shenju)
Fk:loadTranslationTable{
  ["hs__n_simaliang"] = "司马亮",
  ["#hs__n_simaliang"] = "",
  ["designer:hs__n_simaliang"] = "",
  ["illustrator:hs__n_simaliang"] = "",
  
  ["hs__n_gongzhi"] = "共执",
  [":hs__n_gongzhi"] = "你可跳过摸牌阶段并摸一张牌，然后与你势力相同的下一名角色摸一张牌（重复三次）。",
  ["hs__n_shenju"] = "慎惧",
  [":hs__n_shenju"] = "锁定技，与你势力相同的其他角色明置武将牌后，若你已受伤，你回复1点体力并弃置所有手牌。",

  ["~hs__n_simaliang"] = "",
}

local simalun = General(extension, "hs__n_simalun", "jinz", 4)
local zhulan = fk.CreateTriggerSkill{
  name = "hs__n_zhulan",
  events = {fk.DamageInflicted},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player ~= target and H.compareKingdomWith(data.from, target) and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local discard = player.room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#hs__n_zhulan_prompt", true)
    if discard and #discard > 0 then
      self.cost_data = {discard = discard}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data.discard, self.name, player, player)
    data.damage = data.damage + 1
  end,
}
local luanchang = fk.CreateTriggerSkill{
  name = "hs__n_luanchang",
  frequency = Skill.Limited,
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) or player:usedSkillTimes(self.name, Player.HistoryGame) > 0 or target:isKongcheng() then return end
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      if H.compareKingdomWith(p,player) or H.compareExpectedKingdomWith(player,p) then
        if #room.logic:getActualDamageEvents(1, function (e)
          return e.data[1].to == p
        end, Player.HistoryTurn) > 0 then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#hs__n_luanchang_prompt::"..target.id)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:useVirtualCard("archery_attack",target:getCardIds("h"),target,room:getOtherPlayers(target), self.name)
  end
}
simalun:addSkill(zhulan)
simalun:addSkill(luanchang)
Fk:loadTranslationTable{
  ["hs__n_simalun"] = "司马伦",
  ["#hs__n_simalun"] = "螭吻裂冠",
  ["designer:hs__n_simalun"] = "",
  ["illustrator:hs__n_simalun"] = "",
  
  ["hs__n_zhulan"] = "助澜",
  [":hs__n_zhulan"] = "其他角色受到伤害时，若伤害来源与其势力相同，你可弃置一张牌令此伤害+1。",
  ["hs__n_luanchang"] = "乱常",
  [":hs__n_luanchang"] = "限定技，一名角色的回合结束时，若存在与你势力相同本回合受到过伤害且存活的角色，或存在于你明置此武将牌后与你势力相同本回合受到过伤害且存活的角色，你可以令当前回合角色将所有手牌当【万箭齐发】使用。",
  ["#hs__n_zhulan_prompt"] = "助澜：你可弃置一张牌，令此伤害+1",
  ["#hs__n_luanchang_prompt"] = "乱常：你可令 %dest 将所有手牌当【万箭齐发】使用",

  ["~hs__n_simalun"] = "",
}


local shibao = General(extension, "hs__n_shibao", "jinz", 4)
local zhuosheng = fk.CreateTriggerSkill{
  name = "hs__n_zhuosheng",
  events = {fk.AfterCardsMove},
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) and player:getMark("hs__n_zhuosheng_additionalDamage-turn") == 0 then
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(player,"hs__n_zhuosheng_additionalDamage-turn")
  end
}
local zhuosheng_trigger = fk.CreateTriggerSkill{
  name = "#hs__n_zhuosheng_trigger",
  events = {fk.CardUsing},
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("hs__n_zhuosheng_additionalDamage-turn") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:removePlayerMark(player, "hs__n_zhuosheng_additionalDamage-turn")
    player:broadcastSkillInvoke("hs__n_zhuosheng")
    player.room:notifySkillInvoked(player, "hs__n_zhuosheng", "offensive")
    data.additionalDamage = (data.additionalDamage or 0) + 1
  end
}
zhuosheng:addRelatedSkill(zhuosheng_trigger)
shibao:addSkill(zhuosheng)
Fk:loadTranslationTable{
  ["hs__n_shibao"] = "石苞",
  ["#hs__n_shibao"] = "经国之才",
  ["designer:hs__n_shibao"] = "",
  ["illustrator:hs__n_shibao"] = "",
  
  ["hs__n_zhuosheng"] = "擢升",
  [":hs__n_zhuosheng"] = "当你获得牌后，你可以令你本回合使用的下一张牌伤害+1（不能叠加）。",
  ["#hs__n_zhuosheng_trigger"] = "擢升",

  ["~hs__n_shibao"] = "",
}

local yanghuiyu = General(extension, "hs__n_yanghuiyu", "jinz", 3, 3, General.Female)
local ciwei = fk.CreateTriggerSkill{
  name = "hs__n_ciwei",
  anim_type = "control",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player.phase ~= Player.NotActive and target ~= player and player:hasSkill(self) then
      local targets = table.filter(player.room:getOtherPlayers(target, false), function(p) --检测目标
        return not H.compareKingdomWith(p, target) and not p.dead and
          #player.room.logic:getEventsOfScope(GameEvent.UseCard or GameEvent.RespondCard, 1, function(e)
            local use = e.data[1]
            return use.from == p.id
          end, Player.HistoryTurn) > 0
        end)
      return #targets > 0 and U.isUsingHandCard(target, data)
    end
  end,
 on_cost = function(self, event, target, player, data)
    local room = player.room
    local discard = room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#hs__n_ciwei-discard")
    if #discard > 0 then
      self.cost_data = discard
      return true
    end
 end,
 on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data, self.name, player, player)
    data.tos = {}
 end,
}
local caiyuan = fk.CreateTriggerSkill{
  name = "hs__n_caiyuan",
  events = {fk.Damaged, fk.TurnStart},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) or H.getGeneralsRevealedNum(player) ~= 2 then return false end
    return (event == fk.TurnStart and player:getMark("hs__n_caiyuan_shown-turn") == 0 and player == target) or (event == fk.Damaged and player == target)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    if event == fk.Damaged then
      H.hideBySkillName(player, self.name)
    else
      player:drawCards(2, self.name)
    end
  end,

  refresh_events = {fk.GeneralRevealed},
  can_refresh = function (self, event, target, player, data)
    for _, v in pairs(data) do
      if v == "hs__n_yanghuiyu" then 
        return true 
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "hs__n_caiyuan_shown-turn", 1)
  end,
}
yanghuiyu:addSkill(ciwei)
yanghuiyu:addSkill(caiyuan)
Fk:loadTranslationTable{
  ["hs__n_yanghuiyu"] = "羊徽瑜",
  ["#hs__n_yanghuiyu"] = "克明礼教",
  ["designer:hs__n_yanghuiyu"] = "",
  ["illustrator:hs__n_yanghuiyu"] = "",
  
  ["hs__n_ciwei"] = "慈威",
  [":hs__n_ciwei"] = "其他角色于你的回合内使用牌手牌时，若存在不与其势力相同且本回合使用或打出过牌且存活的其他角色，你可弃置一张牌令此牌无效。",
  ["hs__n_caiyuan"] = "才媛",
  [":hs__n_caiyuan"] = "锁定技，回合开始时，若你的武将牌均明置且你本回合未明置过此武将牌，你摸两张牌；当你受到伤害后，若你的武将牌均明置，你暗置此武将牌。",
  ["#hs__n_ciwei-discard"] = "慈威: 你可以弃置一张牌令此牌无效",

  ["~hs__n_yanghuiyu"] = "",
}

local wangyuanji = General(extension, "hs__n_wangyuanji", "jinz",  3, 3, General.Female)
local yanxi_active = fk.CreateActiveSkill{
  name = "#hs__n_yanxi_active",
  mute = true,
  target_num = 0,
  interaction = function (self, player)
    local names = player:getTableMark("hs__n_yanxi-namemark")
    return UI.ComboBox{choices = names, all_choices = names}
  end,
  card_filter = function (self, to_select, selected, player)
    return to_select == player:getMark("hs__n_yanxi-cardmark")
  end,
}
local yanxi = fk.CreateTriggerSkill{
  name = "hs__n_yanxi",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Start
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p)
      return not H.compareKingdomWith(player, p) and not p:isKongcheng()
    end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 3, "#hs__n_yanxi-invoke", self.name)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = self.cost_data.tos
    room:sortPlayersByAction(tos)
    local recordcards, recordnames = {}, {}
    for _, pid in ipairs(tos) do
      local p = room:getPlayerById(pid)
      if not p:isKongcheng() then
        local cid = room:askForCardChosen(player, p, "h", self.name)
        table.insert(recordcards, cid)
      end
    end
    local allCardIds = Fk:getAllCardIds()
    local allCardNames = {}
    for _, id in ipairs(allCardIds) do
      local card = Fk:getCardById(id)
      if not H.convertCards[card.name] then
        table.insertIfNeed(allCardNames, card.trueName)
      end
    end
    if #allCardNames == 0 then return end
    for _, pid in ipairs(tos) do
      local p = room:getPlayerById(pid)
      local n = table.indexOf(tos, pid)
      room:setPlayerMark(p, "hs__n_yanxi-namemark", allCardNames)
      room:setPlayerMark(p, "hs__n_yanxi-cardmark", recordcards[n])
      local result, ret = room:askForUseActiveSkill(p, "#hs__n_yanxi_active", "#hs__n_yanxi-choosename", false)
      local cardName
      if result and ret and ret.interaction then
        cardName = ret.interaction
      else --唉，AI&烧条，开摆
        cardName = table.random(table.simpleClone(allCardNames), 1)[1]
      end
      table.insert(recordnames, cardName)
      room:sendLog{
        type = "#hs__n_yanxi-name",
        from = pid,
        arg = cardName,
        toast = true,
      }
    end
    local t = room:askForChoosePlayers(player, tos, 1, 1, "#hs__n_yanxi-choose", self.name, false)
    if #t == 0 then return end
    local victim = t[1]
    local n = table.indexOf(tos, victim)
    room:getPlayerById(victim):showCards(recordcards[n])
    room:delay(500)
    local same = Fk:getCardById(recordcards[n]).trueName == recordnames[n]
    room:obtainCard(player, recordcards[n], true, fk.ReasonPrey, player.id, self.name)
    if not same then
      table.removeOne(recordcards, recordcards[n])
      recordcards = U.moveCardsHoldingAreaCheck(room, recordcards, room.logic:getCurrentEvent().id)
      if #recordcards > 0 then
        room:obtainCard(player, recordcards, false, fk.ReasonPrey, player.id, self.name)
      end
    end
  end,
}
local shiren = fk.CreateTriggerSkill{
  name = "hs__n_shiren",
  events = {fk.Damaged},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and H.getGeneralsRevealedNum(target) == 0 and player ~= target and #player:getCardIds("he") >= 2
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForCard(player, 2, 2, true, self.name, true, ".", "#hs__n_shiren-invoke::"..target.id)
    if #cards > 0 then
      self.cost_data = {cards = cards}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:obtainCard(target, self.cost_data.cards, false, fk.ReasonGive, player.id, self.name)
    if not player.dead then
      player:drawCards(2, self.name)
    end
  end,
}
yanxi:addRelatedSkill(yanxi_active)
wangyuanji:addSkill(yanxi)
wangyuanji:addSkill(shiren)
Fk:loadTranslationTable{
  ["hs__n_wangyuanji"] = "王元姬",
  ["#hs__n_wangyuanji"] = "垂心万物",
  ["designer:hs__n_wangyuanji"] = "",
  ["illustrator:hs__n_wangyuanji"] = "",
  
  ["hs__n_yanxi"] = "宴戏",
  [":hs__n_yanxi"] = "准备阶段，你可选择至多三名其他势力角色各一张手牌，这些角色依次声明一个牌名，然后你展示并获得其中一名角色被选择的一张牌，若此牌牌名与其声明的不同，你获得其余被选择的牌。",
  ["hs__n_shiren"] = "识人",
  [":hs__n_shiren"] = "每回合限一次，未确定势力的其他角色受到伤害后，你可以交给其两张牌并摸两张牌。",
  ["#hs__n_yanxi-invoke"] = "宴戏：选择至多三名其他势力角色展示其一张手牌",
  ["#hs__n_yanxi-choosename"] = "宴戏：为此牌声明一个牌名",
  ["#hs__n_yanxi_active"] = "宴戏",
  ["#hs__n_yanxi-choose"] = "宴戏：选择一名角色展示并获得牌",
  ["#hs__n_yanxi-name"] = "%from 声明牌名 %arg ",
  ["#hs__n_shiren-invoke"] = "识人：你可交给 %dest 两张牌，然后你摸两张牌",

  ["~hs__n_wangyuanji"] = "",
}

local weiguan = General(extension, "hs__n_weiguan", "jinz", 3)
local chengxi = fk.CreateTriggerSkill{
  name = "hs__n_chengxi",
  anim_type = "drawcard",
  mute = true,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_cost = function(self,event,target,player,data)
    local room = player.room
    local targets = table.map(room.alive_players, Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#hs__n_chenxi-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local user = room:getPlayerById(self.cost_data)
    local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, user) end)
    local targetss = table.map(table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, user) end), Util.IdMapper)
    player:broadcastSkillInvoke(self.name)
    room:notifySkillInvoked(user, self.name, "masochism")
    local preuse = room:useVirtualCard("await_exhausted", {}, user, targets, self.name)
    if preuse and preuse.extra_data and preuse.extra_data.hs__n_chengxi_record then
      local mark = preuse.extra_data.hs__n_chengxi_record
      if #mark > 0 and not player.dead and not user.dead then
        room:sortPlayersByAction(targetss)
        for _, pid in ipairs(targetss) do
          local p = room:getPlayerById(pid)
          if not p.dead then
            room:damage{
              from = user,
              to = p,
              damage = 1,
              skillName = self.name,
            }
          end
        end
      end
    end
  end,
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then return true end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if e then
      local use = e.data[1]
      if table.contains(use.card.skillNames, "hs__n_chengxi") then
        local exdata = e.data[1].extra_data or {}
        exdata.hs__n_chengxi_record = exdata.hs__n_chengxi_record or {}
        for _, move in ipairs(data) do
          if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              local card = Fk:getCardById(info.cardId)
              if card.type ~= Card.TypeBasic then
                table.insertIfNeed(exdata.hs__n_chengxi_record, info.cardId)
              end
            end
          end
        end
        e.data[1].extra_data = exdata
      end
    end
  end,
}
local jiantong = fk.CreateTriggerSkill{
  name = "hs__n_jiantong",
  anim_type = "control",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not p:isKongcheng()
    end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#hs__n_jiantong-invoke", self.name)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toid = self.cost_data.tos[1]
    local to = room:getPlayerById(toid)
    local cards1 = to:getCardIds("h")
    local cards2 = player:getCardIds("e")
    local cards = room:askForPoxi(player, "#hs__n_jiantong-method", {
      { "$Hand", cards1 },
      { "$Equip", cards2 }
    })
    if #cards == 0 then return end
    local mcards1 = table.filter(cards, function(id) return table.contains(cards1, id) end)
    local mcards2 = table.filter(cards, function(id) return table.contains(cards2, id) end)
    U.swapCards(room, player, player, to, mcards2, mcards1, self.name)
  end,
}
Fk:addPoxiMethod{
  name = "#hs__n_jiantong-method",
  card_filter = function(to_select, selected, data)
    if data and #selected < 3 then
      if table.contains(data[1][2], to_select) then
        local n = 0
        for _, id in ipairs(selected) do
          if table.contains(data[1][2], id) then
            n = n + 1
          end
        end
        return n < 2
      else
        local n = 0
        for _, id in ipairs(selected) do
          if table.contains(data[2][2], id) then
            n = n + 1
          end
        end
        return n < 1
      end
    end
  end,
  feasible = function (selected, data, extra_data)
    for _, id in ipairs(selected) do
      if table.contains(data[2][2], id) then
        return true
      end
    end
  end,
  prompt = function ()
    return "#hs__n_jiantong-card"
  end
}
weiguan:addSkill(chengxi)
weiguan:addSkill(jiantong)
Fk:loadTranslationTable{
  ["hs__n_weiguan"] = "卫瓘",
  ["#hs__n_weiguan"] = "忠允清识",
  ["designer:hs__n_weiguan"] = "",
  ["illustrator:hs__n_weiguan"] = "",
  
  ["hs__n_chengxi"] = "乘隙",
  [":hs__n_chengxi"] = "准备阶段，你可令一名角色视为使用一张【以逸待劳】，此牌结算后若以此法弃置的牌中包含非基本牌，其对此牌的所有目标依次造成1点伤害。",
  ["hs__n_jiantong"] = "监统",
  [":hs__n_jiantong"] = "当你受到伤害后，你可观看一名角色所有手牌，然后你可用装备区内一张牌交换其中至多两张牌。",
  ["#hs__n_chenxi-choose"] = "乘隙：选择一名角色，其视为使用【以逸待劳】",
  ["#hs__n_jiantong-invoke"] = "监统：选择一名角色，观看其手牌",
  ["#hs__n_jiantong-card"] = "监统：你可用一张装备牌交换其至多两张牌",
  ["hs__n_jiantong-method"] = "监统",

  ["~hs__n_weiguan"] = "",
}

local jiachong = General(extension, "hs__n_jiachong", "jinz", 3)
local chujue = fk.CreateTargetModSkill{
  name = "hs__n_chujue",
  anim_type = "offensive",
  bypass_times = function (self, player, skill, scope, card, to)
    local death_kingdom = {}
    for _,p in ipairs(table.filter(Fk:currentRoom().players, function(p) return p.dead end)) do
      if H.getKingdom(p) ~= "unknown" then
        table.insertIfNeed(death_kingdom, H.getKingdom(p))
      end
    end
    return to and table.contains(death_kingdom, H.getKingdom(to))
  end
}
local chujue_trigger = fk.CreateTriggerSkill{
  name = "#hs__n_chujue_trigger",
  events = {fk.TargetSpecified},
  mute = true,
  frequency = Skill.Compulsory,
  can_trigger = function (self, event, target, player, data)
    if not (player:hasSkill(self) and player == target) then return false end
    local death_kingdom = {}
    for _, p in ipairs(table.filter(player.room:getAllPlayers(), function(p) return p.dead end)) do
      if H.getKingdom(p) ~= "unknown" then
        table.insertIfNeed(death_kingdom, H.getKingdom(p))
      end
    end
    local target_ban_kingdom = {}
    for _, pid in ipairs(AimGroup:getAllTargets(data.tos)) do
      local k = player.room:getPlayerById(pid).kingdom
      if table.contains(death_kingdom,k) then
        table.insertIfNeed(target_ban_kingdom,k)
      end
    end
    self.cost_data = {kingdoms = target_ban_kingdom}
    return #target_ban_kingdom > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    player.room:notifySkillInvoked(player, "hs__n_chujue")
    player:broadcastSkillInvoke("hs__n_chujue")
    for _, p in ipairs(player.room:getAlivePlayers()) do
      if table.contains(self.cost_data.kingdoms, H.getKingdom(p)) then
        table.insertIfNeed(data.disresponsiveList, p.id)
      end
    end
  end
}
local jianzhi_victim = fk.CreateTriggerSkill{
  name = "#hs__n_jianzhi_victim",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.BuryVictim},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
      return data.damage and data.damage.from and data.damage.from:getMark("hs__n_jianzhi_victims-turn") > 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local deathEvent = room.logic:getCurrentEvent():findParent(GameEvent.Death, true).data[1]
    deathEvent.extra_data = deathEvent.extra_data or {}
    deathEvent.extra_data.threeDoubleawards = true --需要修改hegemony.lua的奖励规则(参考朱灵)
    local targets = table.map(table.filter(room.alive_players, function(p) return not p.dead and p:getMark("hs__n_jianzhi_victims-turn") > 0 end), Util.IdMapper)
    room:sortPlayersByAction(targets)
    for _, pid in ipairs(targets) do
      local p = room:getPlayerById(pid)
      if not p.dead then
       room:setPlayerMark(p,"hs__n_jianzhi_victims-turn", 0)
      end
    end
  end,
}
local jianzhi = fk.CreateTriggerSkill{
  name = "hs__n_jianzhi",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not data.to.dead and data.from == player and data.damage >= data.to.hp and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function(p) return not p.dead end), Util.IdMapper)
    player:throwAllCards("h", self.name)
    room:sortPlayersByAction(targets)
    for _, pid in ipairs(targets) do
      local p = room:getPlayerById(pid)
      if not p.dead then
       room:setPlayerMark(p, "hs__n_jianzhi_victims-turn", 1)
      end
    end
  end,
}
chujue:addRelatedSkill(chujue_trigger)
jianzhi:addRelatedSkill(jianzhi_victim)
jiachong:addSkill(chujue)
jiachong:addSkill(jianzhi)
Fk:loadTranslationTable{
  ["hs__n_jiachong"] = "贾充",
  ["#hs__n_jiachong"] = "",
  ["designer:hs__n_jiachong"] = "",
  ["illustrator:hs__n_jiachong"] = "",
  
  ["hs__n_chujue"] = "除绝",
  [":hs__n_chujue"] = "锁定技，你对存在死亡角色势力的角色使用牌无次数限制且这些角色不能响应之。",
  ["hs__n_jianzhi"] = "奸志",
  [":hs__n_jianzhi"] = "当你造成伤害时，若伤害值大于受伤角色体力值，你可弃置所有手牌，令本回合下次所有角色执行的奖惩改为三倍。",
  ["#hs__n_chujue_trigger"] = "除绝",
  ["#hs__n_jianzhi_victim"] = "奸志",

  ["~hs__n_jiachong"] = "",
}

local guohuai = General(extension, "hs__n_guohuai", "jinz", 3, 3, General.Female)
guohuai:addCompanions("hs__n_jiachong")
local zhefu = fk.CreateTriggerSkill{
  name = "hs__n_zhefu",
  anim_type = "control",
  events = {fk.CardUseFinished, fk.CardRespondFinished},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.NotActive and data.card.type == Card.TypeBasic
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local x = H.getKingdomPlayersNum(room)[H.getKingdom(player)]
    local targets = table.filter(room.alive_players, function (p)
      return not p:isKongcheng() and (H.getKingdomPlayersNum(room)[H.getKingdom(p)] or 1) >= x
    end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#hs__n_zhefu-invoke", self.name)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toid = self.cost_data.tos[1]
    local to = room:getPlayerById(toid)
    local allcards = to:getCardIds("h")
    local cards = table.filter(allcards, function (cid)
      return Fk:getCardById(cid).type == Card.TypeBasic
    end)
    local card, _ = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#hs__n_zhefu-card", nil, 0, 1, allcards)
    if #card > 0 then
      room:throwCard(card, self.name, to, player)
    end
  end,
}
local yidu = fk.CreateTriggerSkill{
  name = "hs__n_yidu",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.is_damage_card
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(TargetGroup:getRealTargets(data.tos), function(id) return
      (not data.damageDealt or (data.damageDealt and not data.damageDealt[id])) and not room:getPlayerById(id):isKongcheng() end)
    if #targets == 0 then return end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#hs__n_yidu-invoke", self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local cards = room:askForCardsChosen(player, to, 1, math.min(2, #to.player_cards[Player.Hand]), "h", self.name)
    to:showCards(cards)
    for _, id in ipairs(cards) do
      if Fk:getCardById(id).color ~= Fk:getCardById(cards[1]).color then
        return
      end
    end
    room:throwCard(cards, self.name, to, player)
  end,
}
guohuai:addSkill(zhefu)
guohuai:addSkill(yidu)
Fk:loadTranslationTable{
  ["hs__n_guohuai"] = "郭槐",
  ["#hs__n_guohuai"] = "嫉贤妒能",
  ["designer:hs__n_guohuai"] = "",
  ["illustrator:hs__n_guohuai"] = "",
  
  ["hs__n_zhefu"] = "哲妇",
  [":hs__n_zhefu"] = "当你于回合外使用或打出基本牌结算后，你可观看一名势力角色数不小于你角色的所有手牌，然后你可弃置其中一张基本牌。",
  ["hs__n_yidu"] = "遗毒",
  [":hs__n_yidu"] = "当你使用伤害类牌结算后，你可展示一名未受到过此牌伤害的目标角色的至多两张手牌，然后若这些牌颜色均相同，弃置之。",
  ["#hs__n_zhefu-invoke"] = "哲妇：选择一名角色，观看其手牌",
  ["#hs__n_zhefu-card"] = "哲妇：你可弃置其中一张基本牌",
  ["#hs__n_yidu-invoke"] = "遗毒：你可以选择一名角色，展示其至多两张手牌，若颜色相同则全部弃置",

  ["~hs__n_guohuai"] = "",
}

local wangjun = General(extension, "hs__n_wangjun", "jinz", 4)
local chengliu = fk.CreateActiveSkill{
  name = "hs__n_chengliu",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  max_phase_use_time = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return #selected == 0 and #Fk.currentRoom():getPlayerById(to_select):getCardIds("e") < #player:getCardIds("e")
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    while true do
      if not target.dead then
        room:damage({
          damage = 1,
          form = player,
          to = target,
          skillName = self.name,
        })
      end
      if player.dead or target.dead then break end
      if room:askForChoice(player, {"hs__n_chengliu-exchange", "Cancel"}, self.name) == "Cancel" then break end
      U.swapCards(room, player, player, target, player:getCardIds("e"), target:getCardIds("e"), self.name, Card.PlayerEquip)
      local targets = table.filter(room:getOtherPlayers(player), function (p)
        return #p:getCardIds("e") < #player:getCardIds("e")
      end)
      if #targets > 0 then
        local t = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#hs__n_chengliu-choose", self.name)
        if #t == 0 then break end
        target = room:getPlayerById(t[1])
      else
        break
      end
    end
  end,
}
wangjun:addSkill(chengliu)
Fk:loadTranslationTable{
  ["hs__n_wangjun"] = "王濬",
  ["#hs__n_wangjun"] = "顺流长驱",
  ["designer:hs__n_wangjun"] = "",
  ["illustrator:hs__n_wangjun"] = "",
  
  ["hs__n_chengliu"] = "乘流",
  [":hs__n_chengliu"] = "出牌阶段限一次，你可对一名装备区内牌数小于你的角色造成1点伤害，然后你可与其交换装备区内所有牌并重复此流程。",
  ["hs__n_chengliu-exchange"] = "交换装备区内所有牌并重复此流程",
  ["#hs__n_chengliu-choose"] = "乘流：选择一名装备区内牌数小于你的角色，对其造成1点伤害",

  ["~hs__n_wangjun"] = "",
}

local malong = General(extension, "hs__n_malong", "jinz", 4)
malong.hidden = true
local zhuanzhan = fk.CreateProhibitSkill{
  name = "hs__n_zhuanzhan",
  is_prohibited = function (self, from, to, card)
    return from:hasShownSkill(self) and card.trueName == "slash" and H.getKingdom(to) ~= "unknown" and table.find(Fk:currentRoom().alive_players,function (p) return H.getKingdom(p) == "unknown" end)
  end
}
local zhuanzhan_targetmod = fk.CreateTargetModSkill{
  name = "#hs__n_zhuanzhan_targetmod",
  bypass_distances = function (self, player, skill, card, to)
    return player:hasShownSkill(self) and card.trueName == "slash" and H.getKingdom(to) == "unknown"
  end
}
local hs__n_xunji = fk.CreateTriggerSkill{
  name = "hs__n_xunji",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self) and data.card.trueName == "slash" and target == player) then return false end
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = player.room:getUseExtraTargets(data)
      if #targets > 0 then
        self.cost_data = targets
        return true
      end
    else
      if data.from == player.id then 
        if data.extra_data and data.extra_data.use_hs__n_xunji then else return end
        local use = room.logic:getCurrentEvent()
        for _, pid in ipairs(TargetGroup:getRealTargets(data.tos)) do
          local times = #room.logic:getActualDamageEvents(1, function (e) 
            local u = e:findParent(GameEvent.UseCard, false, 3)
            return u ~= nil and u.id == use.id and e.data[1].to.id == pid
          end, Player.HistoryPhase, use.id)
          if times == 0 then
            return false
          end
        end
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      local to = player.room:askForChoosePlayers(player, self.cost_data, 1, 2, "#hs__n_xunji-choose:::"..data.card:toLogString(), self.name, true)
      if #to > 0 then
        self.cost_data = to
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      data.extra_data = data.extra_data or {}
      data.extra_data.use_hs__n_xunji = true
      table.forEach(self.cost_data, function(pid) TargetGroup:pushTargets(data.tos, pid) end)
    else
      player:addCardUseHistory("slash", -1)
      room.logic:getCurrentEvent().parent.data[1].extraUse = true
    end
  end,
}
zhuanzhan:addRelatedSkill(zhuanzhan_targetmod)
malong:addSkill(zhuanzhan)
malong:addSkill(hs__n_xunji)
Fk:loadTranslationTable{
  ["hs__n_malong"] = "马隆",
  ["#hs__n_malong"] = "困局诡阵",
  ["designer:hs__n_malong"] = "",
  ["illustrator:hs__n_malong"] = "",
  
  ["hs__n_zhuanzhan"] = "转战",
  [":hs__n_zhuanzhan"] = "锁定技，若存在有未确定势力的角色，你使用【杀】不能指定已确定势力的角色且无距离限制。",
  ["hs__n_hs__n_xunji"] = "勋济",
  [":hs__n_hs__n_xunji"] = "你使用【杀】可多指定两个目标，当你以此法使用【杀】结算后，若此【杀】对所有目标均造成过伤害，此【杀】不计入次数限制。",
  ["#hs__n_zhuanzhan_targetmod"] = "转战",
  ["#hs__n_xunji-choose"] = "勋济：你可以为此 %arg 多指定至多两个目标",

  ["~hs__n_malong"] = "",
}

return extension
